# coding: utf-8
# Author: zhenda

from comment.models import db
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash


class User(db.Model):
    __tablename__ = 't_user'
    id = db.Column(db.BIGINT, primary_key=True, autoincrement=True)  # 必须创建主键！！
    username = db.Column(db.String(64), doc='用户名')  # doc 提示内容
    # 密码需要采用加密的密文， 用flask的 hash算法 128位的
    password = db.Column(db.String(128), doc='密码')
    icon = db.Column(db.String(5000), doc='头像')
    email = db.Column(db.String(64), doc='邮箱')
    nick_name = db.Column(db.String(64), doc='昵称')
    note = db.Column(db.String(500), doc='备注')
    phone = db.Column(db.String(11), doc='手机号')

    # 时间属性的创建
    login_time = db.Column(db.DateTime, default=datetime.now(), doc='登录时间')
    create_time = db.Column(db.DateTime, default=datetime.now(), doc='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now(), onupdate=datetime.now(), doc='修改时间')  # onupdate 自动修改
    status = db.Column(db.Integer, doc='用户状态')  # 0 代码正常

    @property
    def pwd(self):
        return self.password

    @pwd.setter
    def pwd(self, x_password):
        """
        根据明文，转为密文
        :param x_password:明文
        :return: 密文
        """
        self.password = generate_password_hash(x_password)

    def check_password(self, x_password):
        """
        验证密码
        :param x_password:明文
        :return:
        """
        return check_password_hash(self.password, x_password)


if __name__ == '__main__':
    db.create_all()
    # db.drop_all()